package cn.lmxdev.ptms.controller;

import cn.lmxdev.ptms.bean.table.TUser;
import cn.lmxdev.ptms.bean.vo.ResponseVo;
import cn.lmxdev.ptms.service.IUserServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController(value = "userController")
@RequestMapping(value = "/api/user")
public class UserController {
    private final IUserServlet userServlet;

    @Autowired
    public UserController(
            IUserServlet userServlet
    ) {
        this.userServlet = userServlet;
    }

    @PostMapping(value = "/register")
    public ResponseVo<Object> register(
            @RequestParam(value = "departmentId") Long departmentId,
            @RequestParam(value = "specialtyId") Long specialtyId,
            TUser user
    ) {
        this.userServlet.register(departmentId, specialtyId, user);
        return new ResponseVo<>().setCode(0).setMsg("注册成功");
    }

    @GetMapping(value = "/my-info")
    public ResponseVo<Object> myInfo() {
        // 准备数据
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        // 响应数据
        return new ResponseVo<>(this.userServlet.getByAccount(auth.getName()));
    }

    @PutMapping(value = "/edit-my-info")
    public ResponseVo<Object> editMyInfo(TUser user) {
        // 准备数据
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        TUser nowUser = this.userServlet.getByAccount(auth.getName());

        // 更新数据
        this.userServlet.editMyInfo(user.setId(nowUser.getId()));

        // 响应数据
        return new ResponseVo<>().setCode(0).setMsg("操作成功");
    }

    @GetMapping(value = "/my-menu")
    public ResponseVo<Object> myMenu() {
        // 准备数据
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        ArrayList<String[]> menuList = new ArrayList<>();

        // 校验角色
        switch (auth.getAuthorities().toString()) {
            case "[ROLE_ADMIN]" -> {
                menuList.add(new String[]{"学院管理", "/static/page/department/index.html"});
                menuList.add(new String[]{"教师管理", "/static/page/teacher/index.html"});
                menuList.add(new String[]{"学生管理", "/static/page/student/index.html"});
            }
            case "[ROLE_DEPARTMENT-HEAD]" -> {
                menuList.add(new String[]{"专业管理", "/static/page/specialty/index.html"});
                menuList.add(new String[]{"教师管理", "/static/page/teacher/index.html"});
                menuList.add(new String[]{"学生管理", "/static/page/student/index.html"});
            }
            case "[ROLE_SPECIALTY-HEAD]" -> {
                menuList.add(new String[]{"题目管理", "/static/page/topic/index.html"});
                menuList.add(new String[]{"申请管理", "/static/page/specialty-teacher/index.html"});
                menuList.add(new String[]{"教师管理", "/static/page/teacher/index.html"});
                menuList.add(new String[]{"学生管理", "/static/page/student/index.html"});
            }
            case "[ROLE_ADVISER]" -> {
                menuList.add(new String[]{"题目管理", "/static/page/topic/index.html"});
                menuList.add(new String[]{"学生管理", "/static/page/student/index.html"});
                menuList.add(new String[]{"提交管理", "/static/page/student-submit/index.html"});
            }
            case "[ROLE_TEACHER]" -> {
                menuList.add(new String[]{"申请管理", "/static/page/specialty-teacher/index.html"});
            }
            case "[ROLE_STUDENT]" -> {
                menuList.add(new String[]{"题目管理", "/static/page/topic/index.html"});
                menuList.add(new String[]{"提交管理", "/static/page/student-submit/index.html"});
            }
        }

        // 响应数据
        return new ResponseVo<>(menuList);
    }

    @GetMapping(value = "/list")
    public ResponseVo<Object> list(
            @RequestParam(value = "limit", required = false) Long limit,
            @RequestParam(value = "page", required = false) Long page,
            TUser user
    ) {
        return new ResponseVo<>(
                this.userServlet.count(user),
                this.userServlet.list(limit, page, user)
        );
    }

    @GetMapping(value = "/teacher-list")
    public ResponseVo<Object> teacherList(
            @RequestParam(value = "limit", required = false) Long limit,
            @RequestParam(value = "page", required = false) Long page,
            TUser user
    ) {
        // 准备数据
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        TUser nowUser = this.userServlet.getByAccount(auth.getName());

        // 响应数据变量
        long count = 0L;
        Object data = null;

        // 校验角色
        switch (auth.getAuthorities().toString()) {
            case "[ROLE_ADMIN]" -> {
                count = this.userServlet.countByTeacher(user);
                data = this.userServlet.listByTeacher(limit, page, user);
            }
            case "[ROLE_DEPARTMENT-HEAD]" -> {
                count = this.userServlet.departmentHeadCountByTeacher(nowUser.getId(), user);
                data = this.userServlet.departmentHeadListByTeacher(nowUser.getId(), limit, page, user);
            }
            case "[ROLE_SPECIALTY-HEAD]" -> {
                count = this.userServlet.specialtyHeadCountByTeacher(nowUser.getId(), user);
                data = this.userServlet.specialtyHeadListByTeacher(nowUser.getId(), limit, page, user);
            }
            case "[ROLE_ADVISER]" -> {
            }
            case "[ROLE_TEACHER]" -> {
            }
            case "[ROLE_STUDENT]" -> {
            }
        }

        // 响应数据
        return new ResponseVo<>(count, data);
    }

    @GetMapping(value = "/student-list")
    public ResponseVo<Object> studentList(
            @RequestParam(value = "limit", required = false) Long limit,
            @RequestParam(value = "page", required = false) Long page,
            TUser user
    ) {
        // 准备数据
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        TUser nowUser = this.userServlet.getByAccount(auth.getName());

        // 响应数据变量
        long count = 0L;
        Object data = null;

        // 校验角色
        switch (auth.getAuthorities().toString()) {
            case "[ROLE_ADMIN]" -> {
                count = this.userServlet.count(user);
                data = this.userServlet.list(limit, page, user);
            }
            case "[ROLE_DEPARTMENT-HEAD]" -> {
                count = this.userServlet.departmentHeadCountByStudent(nowUser.getId(), user);
                data = this.userServlet.departmentHeadListByStudent(nowUser.getId(), limit, page, user);
            }
            case "[ROLE_SPECIALTY-HEAD]" -> {
                count = this.userServlet.specialtyHeadCountByStudent(nowUser.getId(), user);
                data = this.userServlet.specialtyHeadListByStudent(nowUser.getId(), limit, page, user);
            }
            case "[ROLE_ADVISER]" -> {
                count = this.userServlet.adviserCountByStudent(nowUser.getId(), user);
                data = this.userServlet.adviserListByStudent(nowUser.getId(), limit, page, user);
            }
            case "[ROLE_TEACHER]" -> {
            }
            case "[ROLE_STUDENT]" -> {
                count = this.userServlet.count(user.setId(nowUser.getId()));
                data = this.userServlet.list(limit, page, user.setId(nowUser.getId()));
            }
        }

        // 响应数据
        return new ResponseVo<>(count, data);
    }

    @PostMapping(value = "/add")
    public ResponseVo<Object> add(TUser user) {
        this.userServlet.add(user);
        return new ResponseVo<>().setCode(0).setMsg("操作成功");
    }

    @DeleteMapping(value = "/delete/{id}")
    public ResponseVo<Object> delete(@PathVariable(value = "id") Long id) {
        this.userServlet.remove(id);
        return new ResponseVo<>().setCode(0).setMsg("操作成功");
    }

    @PutMapping(value = "/edit")
    public ResponseVo<Object> edit(TUser user) {
        this.userServlet.edit(user);
        return new ResponseVo<>().setCode(0).setMsg("操作成功");
    }

    @GetMapping(value = "/look/{id}")
    public ResponseVo<Object> look(@PathVariable(value = "id") Long id) {
        return new ResponseVo<>(this.userServlet.get(id));
    }
}
